var vue = new Vue({
    el : '#app',
    data : {
        roleTreeData : [],
        treeProps: {
            children: 'children',
            label: 'roleName'
        },
        menuTreeData: [],
        menuTreeProps:{
            children: 'children',
            label: 'menuName'
        },
        roleMenuForm : {
          roleId : 0,
          menuIds:[],
        },

        loading: false,
    },
    created : function(){
        this.loadAllRole();
        this.loadAllMenu();
    },
    methods : {
        loadAllRole: function () {
            var self = this;
            $.ajax({
                url:  constant.ROOT_SITE + '/system/role/list' ,
                success: function (result) {
                    if (result.code === 200) {
                        self.roleTreeData = result.body;
                    }
                }
            })
        },
        loadAllMenu: function () {
            var self = this;
            $.ajax({
                url:  constant.ROOT_SITE + '/system/menu/list' ,
                success: function (result) {
                    if (result.code === 200) {
                        self.menuTreeData = result.body;
                    }
                }
            })
        },
        clickRole : function(role){
            this.roleMenuForm.roleId = role.id;

            var self = this;
            $.ajax({
                url:  constant.ROOT_SITE + '/system/rolemenu/list/' + role.id,
                success: function (result) {
                    if (result.code === 200) {
                        let menuIds = [];
                        for(let rm of result.body){
                            if(rm.parentId !== '0'){
                                menuIds.push(rm.menuId);
                            }
                        }
                        self.$refs.menuTree.setCheckedKeys(menuIds);
                    }
                }
            })
        },
        savePermission : function(){
            this.roleMenuForm.menuIds = this.$refs.menuTree.getCheckedKeys().concat(this.$refs.menuTree.getHalfCheckedKeys());
            var self = this;
            $.ajax({
                url:  constant.ROOT_SITE + '/system/rolemenu/save',
                data: JSON.stringify(self.roleMenuForm),
                success: function (result) {
                    if (result.code === 200) {
                        self.$message({
                            type: 'success',
                            message: '保存成功！'
                        });
                    }else {
                        self.$message.error(result.errMsg);
                    }
                }
            })
        },

    }
});